/*
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package nsk.jdi.WatchpointRequest.addClassExclusionFilter;

import nsk.share.*;
import nsk.share.jpda.*;
import nsk.share.jdi.*;

import com.sun.jdi.*;
import com.sun.jdi.event.*;
import com.sun.jdi.request.*;

import java.util.*;
import java.io.*;

/**
 * The test for the implementation of an object of the type
 * WatchpointRequest.
 *
 * The test checks that results of the method
 * <code>com.sun.jdi.WatchpointRequest.addClassExclusionFilter()</code>
 * complies with its spec.
 *
 * The test checks up on the following assertion:
 *    Restricts the events generated by this request to those
 *    whose location is in a class
 *    whose name matches a restricted regular expression.
 * The cases to check include both a pattern that begin with '*' and
 * one that end with '*'.
 * - The debugger
 *   - sets up two WatchpointRequests,
 *   - using patterns that begins with '*' and ends with *,
 *     restricts the Requests,
 *   - waits for expected WatchpointEvents.
 * - The debuggee creates and starts two threads, thread1 and thread2,
 *   that being run, throw NullPointerExceptions used
 *   to generate Events and to test the filters.
 * - Upon getting the events, the debugger performs checks required.
 */

public class filter002 extends TestDebuggerType1 {

    public static void main (String argv[]) {
        System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
    }

    public static int run (String argv[], PrintStream out) {
        debuggeeName = "nsk.jdi.WatchpointRequest.addClassExclusionFilter.filter002a";
        return new filter002().runThis(argv, out);
    }

    private String classExclFilter1 = "*filter002aTestClass11";
    private String classExclFilter2 =
      "nsk.jdi.WatchpointRequest.addClassExclusionFilter.filter002aTestClass21*";

    private String classExclName1 = "nsk.jdi.WatchpointRequest.addClassExclusionFilter.filter002aTestClass11";
    private String classExclName2 = "nsk.jdi.WatchpointRequest.addClassExclusionFilter.filter002aTestClass21";

    private String className1 = "nsk.jdi.WatchpointRequest.addClassExclusionFilter.filter002aTestClass10";
    private String className2 = "nsk.jdi.WatchpointRequest.addClassExclusionFilter.filter002aTestClass20";

    protected void testRun() {

        if ( !vm.canWatchFieldModification() ) {
            display("......vm.canWatchFieldModification == false :: test cancelled");
            vm.exit(Consts.JCK_STATUS_BASE);
            return;
        }

        EventRequest eventRequest1 = null;
        EventRequest eventRequest2 = null;

        String property1     = "ModificationWatchpointRequest1";
        String property2     = "ModificationWatchpointRequest2";

        ThreadReference thread1 = null;
        ThreadReference thread2 = null;

        String thread1Name = "thread1";
        String thread2Name = "thread2";

        String fieldName1 = "var101";
        String fieldName2 = "var201";

        ReferenceType testClassReference = null;
        Event newEvent = null;

        for (int i = 0; ; i++) {

            if (!shouldRunAfterBreakpoint()) {
                vm.resume();
                break;
            }

            display(":::::: case: # " + i);

            switch (i) {
                case 0:
                    testClassReference =
                         (ReferenceType) vm.classesByName(className1).get(0);

                    eventRequest1 = setting23ModificationWatchpointRequest (null,
                                           testClassReference, fieldName1,
                                           classExclFilter1,
                                           EventRequest.SUSPEND_ALL, property1);

                    display("......waiting for ModificationWatchpointEvent in tested thread");
                    newEvent = eventHandler.waitForRequestedEvent(new EventRequest[]{eventRequest1}, waitTime, true);

                    if ( !(newEvent instanceof ModificationWatchpointEvent)) {
                        setFailedStatus("ERROR: new event is not ModificationWatchpointEvent");
                    } else {
                        String property = (String) newEvent.request().getProperty("number");
                        display("       got new ModificationWatchpointEvent with property 'number' == " + property);

                        if ( !property.equals(property1) ) {
                            setFailedStatus("ERROR: property is not : " + property1);
                        }

                        String str = ((ModificationWatchpointEvent) newEvent).location().declaringType().name();
                        display("       Class name == " + str);
                        if (str.endsWith(classExclName1)) {
                            setFailedStatus("ERROR: name of Class ends with the testing pattern: " + str);
                        }
                    }
                vm.resume();
                    break;

                case 1:
                    testClassReference =
                         (ReferenceType) vm.classesByName(className2).get(0);

                    eventRequest2 = setting23ModificationWatchpointRequest (null,
                                           testClassReference, fieldName2,
                                           classExclFilter2,
                                           EventRequest.SUSPEND_ALL, property2);

                    display("......waiting for ModificationWatchpointEvent in tested thread");
                    newEvent = eventHandler.waitForRequestedEvent(new EventRequest[]{eventRequest2}, waitTime, true);

                    if ( !(newEvent instanceof ModificationWatchpointEvent)) {
                        setFailedStatus("ERROR: new event is not ModificationWatchpointEvent");
                    } else {
                        String property = (String) newEvent.request().getProperty("number");
                        display("       got new ModificationWatchpointEvent with property 'number' == " + property);

                        if ( !property.equals(property2) ) {
                            setFailedStatus("ERROR: property is not : " + property2);
                        }

                        String str = ((ModificationWatchpointEvent) newEvent).location().declaringType().name();
                        display("       Class name == " + str);
                        if (str.endsWith(classExclName2)) {
                            setFailedStatus("ERROR: name of Class ends with the testing pattern: " + str);
                        }
                    }
                vm.resume();
                    break;

                default:
                    throw new Failure("** default case 2 **");
            }
        }
        return;
    }

    private ModificationWatchpointRequest setting23ModificationWatchpointRequest (
                                                  ThreadReference thread,
                                                  ReferenceType   fieldClass,
                                                  String          fieldName,
                                                  String          classExclFilter,
                                                  int             suspendPolicy,
                                                  String          property        ) {
        try {
            display("......setting up ModificationWatchpointRequest:");
            display("       thread: " + thread + "; fieldClass: " + fieldClass + "; fieldName: " + fieldName);
            Field field = fieldClass.fieldByName(fieldName);

            ModificationWatchpointRequest
            awr = eventRManager.createModificationWatchpointRequest(field);
            awr.putProperty("number", property);

            if (thread != null)
                awr.addThreadFilter(thread);
            awr.setSuspendPolicy(suspendPolicy);
            awr.addClassExclusionFilter(classExclFilter);

            display("      ModificationWatchpointRequest has been set up");
            return awr;
        } catch ( Exception e ) {
           throw new Failure("** FAILURE to set up ModificationWatchpointRequest **");
        }
    }
}
